/**
 * related to selectArea.ui
 * 
 * @Author : chenglm
 * @Timestamp : 2016-07-22
 */
var app = sm("do_App");
var page = sm("do_Page");
var global = deviceone.sm("do_Global");
var hashdata = mm("do_HashData");
var addrList = ui("addrList");
var listdata = mm("do_ListData");
addrList.bindItems(listdata);

var common = require("common");
var communityUrl = require("url/community");
var qqHttp = require("helper/http");
var httpSilence = require("helper/httpSilence");
var param = {};
var communityAddrCount = 1;
var nowAddress = '';
//andriod返回键
page.on("back", function() {
    app.closePage();
});

/* 头部 */
var headerAout = ui("do_ALayout_header");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePage()
});
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "我的社区"
})
header.refreshData();

//页面装载完成后，开始初始化工作
page.on("loaded", function(){
	communityAddrCount = parseInt(global.getMemory("communityAddrCount"));
	global.setMemory("communityAddrCount", communityAddrCount+1);
	deviceone.print('----selectCommunityAddr-communityAddrCount----:'+communityAddrCount);
	//读取当前页面的传入参数
	var jsonData = page.getData();
	param.communityId = jsonData.communityId;
	param.addrId = jsonData.addrId;
	param.address = jsonData.address;
	if (jsonData && jsonData.communityId) {
		qqHttp.post(communityUrl.url.addr.findChildren, {communityId:jsonData.communityId,id:jsonData.addrId}, function(data) {
			deviceone.print('----selectCommunityAddr-loaded-2---:'+JSON.stringify(data));
			if (data.list && data.list.length > 0) {
				listdata.addData(data.list);
			} else {
				listdata.addData([{"template":1,"labelContent":"该社区还没有数据"}]);
			}
			addrList.refreshItems();
		});
	} else {
		qqHttp.post(communityUrl.url.base.allList, {}, function(data) {
			deviceone.print('----selectCommunityAddr-loaded----:'+JSON.stringify(data));
			if (data.list && data.list.length > 0) {
				listdata.addData(data.list);
			} else {
				listdata.addData([{"template":1,"labelContent":"您还没加入任何社区"}]);
			}
			addrList.refreshItems();
		});
	}
});

//点击一条
addrList.on("touch", function(data){
	deviceone.print('---touch-param---:'+JSON.stringify(param));
	var one = listdata.getOne(data);
	deviceone.print('---touch---:'+JSON.stringify(one));
	if (one.communityId) {
		param.communityId = one.communityId;
		param.addrId = one.id;
		nowAddress = one.name;
		param.address = param.address+one.name;
		httpSilence.post(communityUrl.url.addr.isLast, {"id":one.id,"communityId":one.communityId}, function(data2) {
			deviceone.print('---touch-2---:'+JSON.stringify(data2));
			if (data2.isLast == 1) { //是最后一层
				param.fromPage = 'selectCommunityAddr';
				app.closePage(param,null,global.getMemory("communityAddrCount"));
			} else {
				common.open("source://view/area/selectCommunityAddr.ui",param);
			}
		});
	} else if (one.id != null) {
		param.communityId = one.id;
		param.addrId = 0;
		param.address = one.name;
		common.open("source://view/area/selectCommunityAddr.ui",param);
	}
});
page.on("result", function(data) {
	deviceone.print("nowAddress:"+nowAddress);
	param.address = param.address.replace(nowAddress,'');
	communityAddrCount = parseInt(global.getMemory("communityAddrCount"));
	global.setMemory("communityAddrCount", communityAddrCount-1);
});